package com.ch;

import java.util.HashMap;
import java.util.HashSet;

/**
 * @author chenhong
 * @version 1.0.0
 * @ClassName 公平的糖果棒交换.java
 * @Description TODO
 * @createTime 2021年02月01日 15:10:00
 */
public class 公平的糖果棒交换 {
}


//暴力解法
class Solution888 {
    public int[] fairCandySwap(int[] A, int[] B) {
        int[] ans = new int[2];
        int a = 0, b = 0;
        for (int i : A)
            a += i;
        for (int j : B)
            b += j;
        for(int i : A){
            if( i < (a - b) /2)
                continue;
            for(int j : B){
                if((i - j) *2 == (a - b))
                {
                    ans[0] = i;
                    ans[1] = j;
                    return ans;
                }
            }
        }
        return ans;

    }
}


//哈希表
class Solution888_2 {
    public int[] fairCandySwap(int[] A, int[] B) {
        int[] ans = new int[2];
        int a = 0, b = 0;
        for (int i : A)
            a += i;
        for (int j : B)
            b += j;
        HashSet<Integer> hashSet = new HashSet<>();
        for(int i : A){
            hashSet.add(i);
        }
        for(int j : B){
            if(hashSet.contains(j + (a-b)/2)){
                ans[0] = j + (a-b)/2;
                ans[1] = j;
                return ans;
            }
        }
        return ans;

    }
}
